/**********************************************************************
*  Copyright (c) 2007 - Victor Jacobs - victor.jacobs@gmail.com
*
*  Permission is hereby granted, free of charge, to any person
*  obtaining a copy of this software and associated documentation
*  files (the "Software"), to deal in the Software without
*  restriction, including without limitation the rights to use,
*  copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the
*  Software is furnished to do so, subject to the following
*  conditions:
*
*  The above copyright notice and this permission notice shall be
*  included in all copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************/

using System;

namespace Vj.GA.Functions
{
	/// <summary>
	/// Summary description for FunctionCollection.
	/// </summary>
	public class TwoDFunctions
	{
		private static int period = 60;
		public static int Period
		{
			get{return period;}
			set{period = value;}
		}

		private static double [] polyConstants;
		public static double [] PolyConstants
		{
			get	{ return polyConstants;	}
			set { polyConstants = value; }
		}


		public TwoDFunctions()
		{}

		public static double XSquared(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = -x[0]*x[0]/150 + 50;
			return y;
		}

		public static double XCubed(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = x[0]*x[0]*x[0]/10000;
			return y;
		}

		public static double Polynomial(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double a = polyConstants[0];
			double b = polyConstants[1];
			double c = polyConstants[2];

			double y = a*x[0]*x[0] + b*x[0] + c;
			return y;
		}

		
		// period 255, 128, 64, 32, 16
		public static double Sine(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = 50*Math.Sin( x[0]*Math.PI/period);
			return y;
		}

		public static double SineExp(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = 50 * (Math.Sin( Math.Exp( x[0]* Math.PI/period + 3)));
			return y;
		}

		// period = 2, 180
		public static double ExpSine(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = 20*Math.Exp(Math.Sin(x[0]/period));
			return y;
		}

		public static double AbsSine(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = x[0]*Math.Abs(Math.Sin(x[0]* Math.PI/period));
			return y;
		}

		public static double XSine(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = x[0]*Math.Sin(x[0]* Math.PI/period);
			return y;
		}

		public static double OneOverOneMinusX(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y;
			if(x[0] != 0)
				y = 50/(1-x[0]);
			else
				y = 1;
		
			return y;
		}

		public static double OneOverX(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y;
			if(x[0] != 0)
				y = 50/x[0];
			else
				y = 1;
		
			return y;
		}

		public static double Logistic(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = x[0]*(1-x[0]);			
			return y;
		}
		
		public static double Sigmoid(double[] x)
		{
			if(x.Length != 1)
				throw new Exception("Invalid function parameters");

			double y = (50.0 /(1.0 + Math.Exp(-x[0])));
			return y;
		}

	



	}
}
